import 'package:flutter/material.dart';
import 'package:flutterproject/page/mine/children/assets/data/zengzhi_list_bean.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/http/http_client.dart';
import 'package:flutterproject/util/http/http_exception.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/app_bar_common.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

import '../../../../../../util/address_manager.dart';

class ZengZhiHistory extends StatefulWidget {
  @override
  ZengZhiHistoryState createState() => ZengZhiHistoryState();
}

class ZengZhiHistoryState extends State<ZengZhiHistory> {
  ZengzhiListBean? bean;

  HttpClient client = Get.find<HttpClient>();

  @override
  Widget build(BuildContext context) {
    return Container(
      width: Get.width,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.radius),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          AppBarCommon(
            title: "增值记录",
            bgColor: Color(0xFFEDC88B),
            leftColor: Colors.white,
            titleColor: Colors.white,
          ),
          50.heightBox,
          Expanded(
            child: ListView.separated(
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              itemCount: bean?.list?.length ?? 0,
              separatorBuilder: (context, index) => Padding(
                padding: MarginPaddingUtil.symmetric(vertical: 22),
                child: Divider(
                  color: Color(0xFFE7E7E7),
                  height: 1.width,
                ),
              ),
              itemBuilder: (context, index) {
                return Padding(
                  padding: MarginPaddingUtil.symmetric(horizontal: 47),
                  child: Row(
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          TextCommon(
                            bean?.list![index].title ?? "",
                            color: Color(0xFF414141),
                            size: 24.sp,
                            lineHeight: 35.sp,
                          ),
                          17.heightBox,
                          TextCommon(
                            DateFormat("yyyy-MM-dd HH:mm:ss").format(
                                DateTime.fromMillisecondsSinceEpoch(
                                    bean?.list![index].createTime ?? 0)),
                            size: 20.sp,
                            color: Color(0xFF707070),
                            lineHeight: 29.sp,
                          ),
                        ],
                      ),
                      Spacer(),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          TextCommon(
                            "${bean?.list![index].costAmount}",
                            color: Color(0xFF414141),
                            size: 32.sp,
                            fontWeight: FontWeight.bold,
                            lineHeight: 46.sp,
                          ),
                          17.heightBox,
                          TextCommon(
                            "${bean?.list![index].releasedAmount}/${bean?.list![index].totalAmount}",
                            size: 20.sp,
                            color: Color(0xFF707070),
                            lineHeight: 29.sp,
                          ),
                        ],
                      ),
                    ],
                  ),
                );
              },
            ),
          )
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _getConfig();
  }

  Future<bool> _getConfig() async {
    try {
      DialogUtil.showLoading();
      bean = await client.get<ZengzhiListBean>(
        AddressManager.zengzhiHistory,
      );
      setState(() {});
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }
}
